/**
 * desc: menu
 * author: Joe
 * date: 8/3/2018
 */

coreModule.component('nMenu', {
    templateUrl: 'components/menu/menu.template.html',
    bindings: {
      menuList: '<',
      autoOpen: '<'
    },
    controller: [
      '$log', '$timeout', '$location', '$rootScope',
      function ($log, $timeout, $location, $rootScope) {
        function autoCollapse(menuList, path) {
          var $menu = $('#accordion');
          for (var i = 0; i < menuList.length; i++) {
            var menu = menuList[i];
            var submenu = menu.children;
            if (submenu && angular.isArray(submenu)) {
              for (var j = 0; j < submenu.length; j++) {
                if (submenu[j].url === path) {
                  $menu.find('.link').eq(i).click();
                  break;
                }
              }
            }
          }
        }

        var self = this;
        var Accordion = function (el, multiple) {
          this.el = el || {};
          this.multiple = multiple || false;

          var links = this.el.find('.link');
          links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
        };
        Accordion.prototype.dropdown = function (e) {
          var $el = e.data.el;
          var $this = $(this);
          var $next = $this.next();

          if (!$this.hasClass('menu-link')) {
            e.stopPropagation();
          }

          $next.slideToggle();
          $this.parent().toggleClass('open');

          if (!e.data.multiple) {
            $el.find('.submenu').not($next).slideUp().parent().removeClass('open');
          }
        };
        this.$onInit = function () {
          self.path = $location.url().split('?')[0];
          if (!angular.isArray(this.menuList)) {
            this.menuList = [
              {
                title: 'No Menu'
              }
            ];
          }else{
            for (var i = 0; i < this.menuList.length; i++){
              if (this.menuList[i].url !== null){
                if (this.menuList[i].url.indexOf("http://")){
                  // this.menuList[i].url = '#!'+this.menuList[i].url;
                } else {
                  this.menuList[i].target = "_blank";
                }
              }
            }
          }
        };

        this.$onChanges = function() {
          if (angular.isArray(this.menuList)) {
            $timeout(function() {
              var $menu = $('#accordion');
              $menu.find('.link').off('click');
              new Accordion($menu, false);

              if (self.autoOpen) {
                autoCollapse(self.menuList, self.path);
              }

            });
          }
        };
        $rootScope.$on('$locationChangeSuccess', function (event, newUrl) {
          var index = newUrl.indexOf('#!');
          if (index > -1) {
            self.path = newUrl.substr(index + 2);
          }

          var selFpath = self.path.split('?');
          self.path = selFpath[0];
          if (!angular.isUndefined(selFpath[1])){
            autoCollapse(self.menuList, self.path);
          }
        });
      }
    ],
  }
);
